<?php

include('include/connexionBdd.php');
$connexion = ConnexionBdd();
if (isset($_POST['email']) && isset($_POST['mdp'])) {

    $email = $_POST['email'];
    $mdp = $_POST['mdp'];
    $mdp_crypte = md5($mdp);

    $sql = "SELECT * 
      FROM utilisateur
      WHERE emailUtilisateur = '$email'
      AND mdpUtilisateur = '$mdp_crypte'";


    $req = $connexion->query($sql);
    $ligne = $req->fetch();

    if (isset($ligne['idUtilisateur'])) {
        if ($ligne['actif'] == 1) {
            session_start();
            $_SESSION['login'] = $email;
            $_SESSION['nom'] = $ligne['nomUtilisateur'];
            $_SESSION['prenom'] = $ligne['prenomUtilisateur'];
            $_SESSION['pwd'] = $mdp_crypte;

            setcookie('email', $email, time() + 24 * 3600, null, null, false, true);
            setcookie('mdp', $mdp_crypte, time() + 24 * 3600, null, null, false, true);


            echo "<img src='assets/image/valide.png' class='vertical_image' width='30px'> <span class='auth_reussi'><b>Bienvenue " . $ligne['nomUtilisateur'] . " " . $ligne['prenomUtilisateur'] . " !</b></span>";
        } else {
            echo "<span class='auth_fail'><img src='assets/image/attention.png' class='vertical_image' width='30px'> <b>Votre compte n'est pas activé !</b></span>";
        }
    } else {
        echo "<span class='auth_fail'><img src='assets/image/attention.png' class='vertical_image' width='30px'> <b>Mauvais login / mot de passe!</b></span>";
    }
}
